package com.iussoft.receiveData.interceptor;

import com.apache.tools.StrUtil;
import com.iussoft.es.config.ElasticsearchConfig;
import com.iussoft.es.config.EsClientFactory;
import com.iussoft.kafka.KafkaConfig;
import com.iussoft.tools.SendMsgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @description: 统一读取配置
 * @author: Hou Dayu
 * @date: Created in 2020/3/1
 */
@Configuration
public class CommonConfig {

    @Autowired
    private Environment env;

    /**
     * 实例ES相关配置
     */
    @Bean("elasticsearchConfig")
    public ElasticsearchConfig getElasticsearchConfig(){
        ElasticsearchConfig config = new ElasticsearchConfig();
        config.setJestHost(env.getProperty("elasticsearch.jestHost"));
        config.setMaxTotal(Integer.valueOf(StrUtil.doNull(env.getProperty("elasticsearch.maxTotal"),"0")));
        config.setConnTimeout(Integer.valueOf(StrUtil.doNull(env.getProperty("elasticsearch.connTimeout"),"1500")));
        config.setReadTimeOut(Integer.valueOf(StrUtil.doNull(env.getProperty("elasticsearch.readTimeOut"),"20000")));
        config.setCheckRunnable(env.getProperty("elasticsearch.checkRunnable"));
        config.setNumberOfReplicas(StrUtil.doNull(env.getProperty("elasticsearh.numberOfReplicas"),"0"));
        return config;
    }

    /**
     * 实例kafka相关配置
     */
    @Bean("kafkaConfig")
    public KafkaConfig getKafkaConfig(){
        String ifOpenSync = StrUtil.doNull(env.getProperty("kafka.enabled"),"0");
        KafkaConfig config = new KafkaConfig();
        config.setConfig(env);
        config.setEnabled(ifOpenSync);
        return config;
    }

    /**
     * 实例ES连接
     */
    @Bean
    public EsClientFactory getEsClientFactory(){
        EsClientFactory.jestClientInit(getElasticsearchConfig());
        return null;
    }

    @Bean
    public SendMsgTool sendMsgTool(){
        return new SendMsgTool();
    }
}
